package zj.com.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
import zj.com.adapter.ViewPagerAdapter;
import zj.com.net.Urls;
import zj.com.util.ImageUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/5/31.
 */
public class LookImgActivity extends  BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager vp;
    private ViewPagerAdapter vpAdapter;
    private List<View> views;

    public TextView title_name;
    public ImageView left_iv;


    //引导图片资源
    private static final int[] pics = { R.drawable.test_prj,
            R.drawable.sg_test_1};
    private int currentIndex;
    public ProgressBar pb;
    public List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_img);

        pb = (ProgressBar) findViewById(R.id.pb);
        views = new ArrayList<View>();
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("图片查看");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

         DisplayImageOptions options = ImageUtil.getOption();

        list = (List<String>) getIntent().getSerializableExtra("imgs");
        //初始化引导图片列表
        for(int i=0; i<list.size(); i++) {

            String url = list.get(i);
            String index = url.replace(Urls.GetResourceContent, "");
            String newUrl = String.format(Urls.GetImg, index, "Max");

            View view = getLayoutInflater().inflate(R.layout.lookimg,null);
            ImageView photoView = (ImageView) view.findViewById(R.id.iv_photo);


            ImageLoader.getInstance()
                    .displayImage(newUrl, photoView, options, new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {


                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                            pb.setVisibility(View.GONE);
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                            pb.setVisibility(View.GONE);

                        }
                    }, new ImageLoadingProgressListener() {
                        @Override
                        public void onProgressUpdate(String imageUri, View view, int current, int total) {
                        }
                    });
            views.add(view);
        }
        vp = (ViewPager) findViewById(R.id.viewpager);
        //初始化Adapter
        vpAdapter = new ViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        //绑定回调
        vp.setOnPageChangeListener(this);
        int position =  getIntent().getIntExtra("position",-1);
        if(position >=0 ){

            vp.setCurrentItem(position);
        }

        title_name.setText("图片查看("+(position+1)+"/"+list.size()+")");
    }

    public Intent intent ;
    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    /**
     *设置当前的引导页
     */
    private void setCurView(int position)
    {
        if (position < 0 || position >= pics.length) {
            return;
        }

        vp.setCurrentItem(position);
    }

    @Override
    public void onPageSelected(int position) {

        title_name.setText("图片查看("+(position+1)+"/"+list.size()+")");

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
